package com.zerosky.crm.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.zerosky.crm.dao.CustomerDAO;
import com.zerosky.crm.po.Customer;
import com.zerosky.crm.service.CustomerService;
import com.zerosky.crm.util.PageInfo;

/**
 * Customer业务逻辑实现类
 * 
 * @author hugy;
 * @since 2017-06-08
 *
 */
@Service(value="customerService")
public class CustomerServiceImpl implements CustomerService {

	@Autowired
	private CustomerDAO customerDAO; // Customer数据接口

	/**
	 * Customer列表
	 * 
	 * @author hugy;
	 * @since 2017-06-08
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public List<Customer> getCustomerList(Map map) {
		return customerDAO.getCustomerList(map);
	}

	/**
	 * 分页查询信息
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public PageInfo<Customer> getCustomerPageInfoList(Map map, PageInfo<Customer> page) {
		int begin = ((page.getPageNo() - 1) * page.getPageSize());
		int pageSize = page.getPageSize();

		// 写入数据
		map.put("pageNo", begin);
		map.put("pageSize", pageSize);
		List<Customer> list = customerDAO.getCustomerList(map);
		page.setList(list);

		int count = customerDAO.countCustomer(map);// 获取总记录
		page.setPageCount(count);
		return page;
	}

	/**
	 * 通过主键查找Customer实体
	 * 
	 * @param id
	 *            Customer主键
	 * @author hugy;
	 * @since 2017-06-08
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public Customer getCustomerById(String id) {
		return customerDAO.getCustomerById(id);
	}

	/**
	 * 保存Customer
	 * 
	 * @param Customer
	 *            实体对象, operation 操作类型
	 * @author hugy;
	 * @since 2017-06-08
	 */
	@Transactional(propagation = Propagation.REQUIRED) // 创建一个事务，如果有则加入事务
	public int saveCustomer(Customer customer, String operation) throws Exception {
		if ("新增".equals(operation)) {
			return customerDAO.addCustomer(customer);
		} else if ("修改".equals(operation)) {
			return customerDAO.updateCustomer(customer);
		}else{
			return 0;
		}
	}

	/**
	 * 删除Customer
	 * 
	 * @param idArray
	 *            Customer主键数组
	 * @author hugy;
	 * @since 2017-06-08
	 */
	@Transactional(propagation = Propagation.REQUIRED) // 创建一个事务，如果有则加入事务
	public void deleteCustomerById(String id) throws Exception {
		customerDAO.deleteCustomerById(id);
	}

	/**
	 * 其他部门客户数
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public PageInfo<Customer> getOtherDeptCustomerNumber(Map map, PageInfo<Customer> page) {
		int begin = ((page.getPageNo() - 1) * page.getPageSize());
		int pageSize = page.getPageSize();

		// 写入数据
		map.put("pageNo", begin);
		map.put("pageSize", pageSize);
		List<Customer> list = customerDAO.getOtherDeptCustomerNumber(map);
		page.setList(list);

		int count = customerDAO.getOtherDeptCustomerNumberCount(map);// 获取总记录
		page.setPageCount(count);
		return page;
	}

	/**
	 * 其他部门客户总数
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public Integer getOtherDeptCustomerNumberSum(Map map) {
		// 写入数据
		int count = 0;
		count = customerDAO.getOtherDeptCustomerNumberSum(map);// 获取总记录
		return count;
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public int getBirthdayCount(Map map) {
		return customerDAO.countCustomer(map);
	}

	@Override
	public Customer validateCustomer(Map map) {
		return customerDAO.validateCustomer(map);
	}

	@Override
	public void addCustomerList(List<Customer> customer) throws Exception {
		customerDAO.addCustomerList(customer);
	}

	@Override
	public Customer customerLogin(Map map) {
		return customerDAO.customerLogin(map);
	}

	@Override
	public int countCustomer(Map map) {
		return customerDAO.countCustomer(map);
	}
}
